package com.ruoyi.syf.task.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置
 */
@Configuration
public class SchedulingConfig {
    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        // 定时任务执行线程池核心线程数
        taskScheduler.setPoolSize(50);
        taskScheduler.setRemoveOnCancelPolicy(true);
        //线程名称
        taskScheduler.setThreadNamePrefix("TaskSchedulerThreadPool-");
        //等待时长，超出时间强制销毁，防止长时间占用导致线程阻塞
        taskScheduler.setAwaitTerminationSeconds(60 * 3);
        // 线程池对拒绝任务(无线程可用)的处理策略
        taskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return taskScheduler;
    }
}
